package com.bonc.modules.survey.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON;
import com.bonc.modules.survey.service.SurveyItemService;
import com.bonc.modules.target.entity.RptServDyglKdResultMonEntity;
import com.bonc.modules.target.entity.RptServDyglPersonResultMonEntity;
import com.bonc.modules.target.service.MysqlDataSourceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/survey")
public class SurveyItemController{

     // 集团SMS手机资费套餐调研满意度导入数据
     private static String smsJTPhonePrcid = "PDATA3.TB_SMS_JT_PHONE_PRCID_SURVEYRESULT_MON";
     //集团SMS手机上网质量调研满意度导入数据
     private static String smsJTPhoneNet = "PDATA3.TB_SMS_JT_PHONE_NET_SURVEYRESULT_MON";
     //集团SMS手机语音通话调研满意度导入数据
     private static String smsJTPhoneVoc = "PDATA3.TB_SMS_JT_PHONE_VOC_SURVEYRESULT_MON";
     //集团SMS家宽上网质量调研满意度导入数据
     private static String smsJTKDNet = "PDATA3.TB_SMS_JT_KD_NET_SURVEYRESULT_MON";
     //集团SMS互联网电视调研满意度导入数据
     private static String smsJTNetVideo = "PDATA3.TB_SMS_JT_NET_VIDEO_SURVEYRESULT_MON";

     //套餐变更规范性调研问卷导入数据
     private static String servPackageStandard = "PDATA3.TB_RPT_SERV_PACKAGE_STANDARD_RESULT_MON";
     //合约办理规范性调研问卷导入数据
     private static String servContractStandard ="PDATA3.TB_RPT_SERV_CONTRACT_STANDARD_RESULT_MON";
/**
     //集团CATI手机客户调研满意度
     private static String catiJTPhoneCust = "PDATA3.TB_CATI_PHONE_CUST_SURVEY_ITEM_MON";
     //集团CATI家宽客户调研满意度指标导入数据
     private static String catiJTKDCust = "PDATA3.TB_CATI_KD_CUST_SURVEY_ITEM_MON";
     //集团CATI5G客户调研满意度指标导入
     private static String catiJT5GCust = "PDATA3.TB_CATI_5G_CUST_SURVEY_ITEM_MON";
     //集团CATI政企客户调研满意度指标导入
     private static String catiJTZQCust = "PDATA3.TB_CATI_GOV_CUST_SURVEY_ITEM_MON";

     //我省电话调研-手机客户
     private static String catiPhoneCust = "PDATA3.TB_CATI_PHONE_CUST_SURVEYRESULT_MON";
     //我省电话调研-家宽客户
     private static String catiKDCust = "PDATA3.TB_CATI_KD_CUST_SURVEYRESULT_MON";
*/

     @Autowired
     private SurveyItemService surveyItemService;
     @Autowired
     private MysqlDataSourceService mysqlDataSourceService;

     /**
      *  满意度导入明细导出
      * @param response
      * @param
      * @throws Exception
      */
     @GetMapping("surveyInfoExport")
     public void surveyInfoExport(HttpServletResponse response, @RequestParam String  survey,@RequestParam String  dealDate, @RequestParam String  regionCode) throws Exception {
//        try {
          Map<String,Object> param = new HashMap<String,Object>();

          if(StringUtils.isNotBlank(dealDate)){
               param.put("dealDate",dealDate);
          }
          //查询地市信息
          if(StringUtils.isNotBlank(regionCode)){
               param.put("regionCode",regionCode);
               List<Map<String,String>> region =  this.surveyItemService.queryRegionGbase(param);
               param.put("regionDesc",region.get(0).get("regionDesc").replace("地区",""));
          }

          String fileName="";
          if(survey.equals("smsJTPhonePrcid")){
               param.put("colGroup",smsJTPhonePrcid);
               fileName="集团SMS手机资费套餐调研满意度导入明细";

          }else if(survey.equals("smsJTPhoneNet")){
               param.put("colGroup",smsJTPhoneNet );
               fileName="集团SMS手机上网质量调研满意度导入明细";

          }else if(survey.equals("smsJTPhoneVoc")){
               param.put("colGroup",smsJTPhoneVoc);
               fileName="集团SMS手机语音通话调研满意度导入明细";

          }else if(survey.equals("smsJTKDNet")){
               param.put("colGroup",smsJTKDNet);
               fileName="集团SMS家宽上网质量调研满意度导入明细";

          }else if(survey.equals("smsJTNetVideo")) {
               param.put("colGroup", smsJTNetVideo);
               fileName = "集团SMS互联网电视调研满意度导入明细";

          }else if(survey.equals("servPackageStandard")){
               param.put("colGroup", servPackageStandard);
               fileName = "套餐变更规范性调研问卷导入明细";
          }else if(survey.equals("servContractStandard")){
               param.put("colGroup", servContractStandard);
               fileName = "合约办理规范性调研问卷导入明细";
          }
//          else if(survey.equals("catiJTPhoneCust")) {
//               param.put("colGroup", catiJTPhoneCust);
//               fileName = "集团CATI手机客户调研满意度导入明细";
//
//          }else if(survey.equals("catiJTKDCust")) {
//               param.put("colGroup", catiJTKDCust);
//               fileName = "集团CATI家宽客户调研满意度导入明细";
//
//          }else if(survey.equals("catiJT5GCust")) {
//               param.put("colGroup", catiJT5GCust);
//               fileName = "集团CATI5G客户调研满意度导入明细";
//
//          }else if(survey.equals("catiJTZQCust")) {
//               param.put("colGroup", catiJTZQCust);
//               fileName = "集团CATI政企客户调研满意度导入明细";
//
//          }else if(survey.equals("catiPhoneCust")) {
//               param.put("colGroup", catiPhoneCust);
//               fileName = "我省电话调研-手机客户满意度导入明细";
//
//          }else if(survey.equals("catiKDCust")) {
//               param.put("colGroup", catiKDCust);
//               fileName = "我省电话调研-家宽客户满意度导入明细";
//
//          }

          // 获取导出的表头
          List<List<String>> header = new ArrayList<List<String>>();

          List<String> headerList = new ArrayList<String>();
          List<String> columnList = new ArrayList<String>();

          List<Map<String,Object>> tableInfoList =  this.mysqlDataSourceService.getImportTableInfo(param);
          for (Map<String,Object> map: tableInfoList) {
               headerList.add(map.get("colName")+"");
               columnList.add(map.get("colCode")+"");
          }

          for (String str : headerList){
               List<String> list = new ArrayList<String>();
               list.add(str);
               header.add(list);
          }

          response.setContentType("application/vnd.ms-excel");
          response.setCharacterEncoding("utf-8");
          fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
          response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");


          List<List<String>> exportList = new ArrayList<List<String>>();
          List<Map<String,Object>> surveyList =   this.surveyItemService.querySurveyList(param);
          for (Map<String,Object> map : surveyList) {
               List<String> list =new ArrayList<String>();
               for (String colums : columnList) {
                    list.add(map.get(colums)==null?"":map.get(colums)+"");
               }
               exportList.add(list);
          }
          EasyExcel.write(response.getOutputStream()).head(header).autoCloseStream(Boolean.TRUE).sheet(fileName)
                  .doWrite(exportList);

//        } catch (Exception e) {
//            // 重置response
//            response.reset();
//            response.setContentType("application/json");
//            response.setCharacterEncoding("utf-8");
//            response.getWriter().println(JSONUtil.toJsonStr(Result.error("下载文件失败,请联系管理员")));
//        }
     }

     /**
      *  多sheet明细导出
      * @param response
      * @param
      * @throws Exception
      */
     @GetMapping("servSheetInfoExport")
     public void servSheetInfoExport(HttpServletResponse response, @RequestParam String  survey,@RequestParam String  dealDate, @RequestParam String  regionCode) throws Exception {

          Map<String,Object> param = new HashMap<String,Object>();

          if(StringUtils.isNotBlank(dealDate)){
               param.put("dealDate",dealDate);
          }
          //查询地市信息
          if(StringUtils.isNotBlank(regionCode)){
               param.put("regionCode",regionCode);
               List<Map<String,String>> region =  this.surveyItemService.queryRegionGbase(param);
               param.put("regionDesc",region.get(0).get("regionDesc").replace("地区",""));
          }

          String fileName="资费变更范性调研问卷导入明细";


          // 获取导出的表头
          List<List<String>> sheet1Header = new ArrayList<List<String>>();
          List<List<String>> sheet2Header = new ArrayList<List<String>>();

          List<String> headerList = new ArrayList<String>();
          List<String> sheet1ColumnList = new ArrayList<String>();
          List<String> sheet2ColumnList = new ArrayList<String>();

          // sheet1
          param.put("colGroup", servPackageStandard);
          List<Map<String,Object>> tableInfoList =  this.mysqlDataSourceService.getImportTableInfo(param);
          for (Map<String,Object> map: tableInfoList) {
               headerList.add(map.get("colName")+"");
               sheet1ColumnList.add(map.get("colCode")+"");
          }

          for (String str : headerList){
               List<String> list = new ArrayList<String>();
               list.add(str);
               sheet1Header.add(list);
          }

          List<List<String>> sheet1exportList = new ArrayList<List<String>>();
          List<Map<String,Object>> sheet1List =   this.surveyItemService.querySurveyList(param);
          for (Map<String,Object> map : sheet1List) {
               List<String> list =new ArrayList<String>();
               for (String colums : sheet1ColumnList) {
                    list.add(map.get(colums)==null?"":map.get(colums)+"");
               }
               sheet1exportList.add(list);
          }

          // sheet2
          param.put("colGroup", servContractStandard);
          tableInfoList =  this.mysqlDataSourceService.getImportTableInfo(param);
          for (Map<String,Object> map: tableInfoList) {
               headerList.add(map.get("colName")+"");
               sheet2ColumnList.add(map.get("colCode")+"");
          }
          for (String str : headerList){
               List<String> list = new ArrayList<String>();
               list.add(str);
               sheet2Header.add(list);
          }

          List<List<String>> sheet2exportList = new ArrayList<List<String>>();
          List<Map<String,Object>> sheet2List =   this.surveyItemService.querySurveyList(param);
          for (Map<String,Object> map : sheet2List) {
               List<String> list =new ArrayList<String>();
               for (String colums : sheet2ColumnList) {
                    list.add(map.get(colums)==null?"":map.get(colums)+"");
               }
               sheet2exportList.add(list);
          }

          response.setContentType("application/vnd.ms-excel");
          response.setCharacterEncoding("utf-8");
          fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
          response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");


          ExcelWriter build = EasyExcel.write(response.getOutputStream()).build();
          WriteSheet sheet1 = EasyExcel.writerSheet(0, "套餐变更规范性调研问卷导入明细").head(sheet1Header).build();
          WriteSheet sheet2 = EasyExcel.writerSheet(1, "合约办理规范性调研问卷导入明细").head(sheet2Header).build();
          build.write(sheet1exportList,sheet1).write(sheet2exportList,sheet2);
          build.finish();

     }
}
